﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Service;

namespace Print
{
    public partial class SystemManage : Form
    {
        public string Path;
        IWindowAPI WindowsAPI = null;
        IWebRequest WebRequest = null;
        IFile FileIO = null;
        IOCR OCR = null;

        [DllImport("kernel32")]//ini读
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]//ini写
        private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);

        public SystemManage()
        {
            WebRequest = new WebRequestService();
            WindowsAPI = new WindowsAPIService();
            FileIO = new FileService();
            OCR = new OCRService();
            InitializeComponent();
            var data=   FileIO.ReeadTXT("Hangxin");
            if (data.Count>0)
            {
                if (!String.IsNullOrWhiteSpace(data[0]))
                {
                    textBox1.Text = data[0];
                }
                if (2<=data.Count)
                {
                    if (data[1] != null && !String.IsNullOrWhiteSpace(data[1]))
                    {
                        textBox2.Text = data[1];
                    }
                }
                if (3 <= data.Count)
                {
                    if (data[2] != null && !String.IsNullOrWhiteSpace(data[2]))
                    {
                        textBox4.Text = data[2];
                    }
                }
                if (4 <= data.Count)
                {
                    if (data[3] != null && !String.IsNullOrWhiteSpace(data[3]))
                    {
                        textBox5.Text = data[3];
                    }
                }

            }

        }

        //private string strFilePath = Application.StartupPath + "\\System.ini";//获取INI文件路径
        //private string strSec = ""; //INI文件名

        private void HangxinPartmenter_Click(object sender, EventArgs e)
        {

            if (!String.IsNullOrWhiteSpace(textBox1.Text)&&textBox1.Text!= "(必填)")
            {
                FileIO.WriteTXT("Hangxin", new List<string>() { textBox1.Text + " ", textBox2.Text + " ", textBox4.Text + " ", textBox5.Text + " " });
                StaffData.StaffNo = textBox1.Text;
                DialogResult dr = MessageBox.Show("公司信息修改成功", "系统设置提醒");

                if (dr == DialogResult.OK)//如果点击“确定”按钮
                {
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("公司名称必填!", "系统设置提醒");
            }
            

        }
        public void IniWriteValue(string section, string key, string iValue)
        {
            WritePrivateProfileString(section, key, iValue, this.Path);
        }
        public string IniReadValue(string section, string key)
        { 
            StringBuilder temp = new StringBuilder(255); 

            int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path); 
            return temp.ToString();
        }
        public byte[] IniReadValues(string section, string key)
        {
            byte[] temp = new byte[255];

            int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
            return temp;
        }

        private void SystemManage_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(StaffData.StaffNo))
            {
                MessageBoxButtons mb = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show("退出后.公司名称为空将影响每张发票开具", "系统设置提醒", mb);
                if (dr==DialogResult.OK)
                {
                       
                }
                else
                {
                    e.Cancel = true;
                }
            }

        }
    }

}
